"use strict";
const nodemailer = require("nodemailer");

// async..await is not allowed in global scope, must use a wrapper

// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
//   let testAccount = await nodemailer.createTestAccount();

// create reusable transporter object using the default SMTP transport

//创建邮件发送对象
// let transporter: any = nodemailer.createTransport({
//   host: "smtp.qq.com",  //发送邮箱
//   port: 465, //邮箱端口号
//   secure: true, // true for 465, false for other ports
//   auth: {
//     user: '1239184745@qq.com', // generated ethereal user  发送的邮箱地址
//     pass: 'sghycejeggmibabi' // generated ethereal password   mtp验证码
//   }
// });

// let transporter1: any = nodemailer.createTransport({
//   host: "smtp.163.com",  //发送邮箱
//   port: 465, //邮箱端口号
//   secure: true, // true for 465, false for other ports
//   auth: {
//     user: '18381140525@163.com', // generated ethereal user  发送的邮箱地址
//     pass: 'WOYPWTAXHOHEFMTZ' // generated ethereal password   mtp验证码
//   }
// });

//邮箱信息
// let ObjMeali = {
//   from: '"Nodejs -nodemailer"<1239184745@qq.com>', // sender address
//   to: "1239184745@qq.com,321071784@qq.com", // list of receivers  发送地址 可以群发 也可以单独发
//   subject: "Nodejs 验证码", // Subject lineqq.con
//   text: "你的验证码为:12346,有效期为10分钟", // plain text body
//   html: "<h1>Hello world?</h1>" // html body
// }
// send mail with defined transport object

// let ObjMeali = {
//   from: '<18381140525@163.com>', // sender address
//   to: "18381140525@163.com", // list of receivers  发送地址 可以群发 也可以单独发
//   subject: "Nodejs 验证码", // Subject lineqq.con
//   text: "你的验证码为:12346,有效期为10分钟", // plain text body
//   // html: "<b>Hello world?</b>" // html body
// }

//发送邮箱
// transporter.sendMail(ObjMeali,(e:Object,info:object)=>{
//     console.log(e)
//     if(e){
//       return e
//     }
//     console.log(info)
// });

//===========================================
interface getdata {
  sendout: string | undefined; //发件人信息
  addressee: Array<string> | string | undefined; //发送人 可以单个，以字符串形式，可以为多个，为数组格式的字符串
  sendtitle: string | undefined; //发送标题
  sendtxt?: string | undefined;//发送文本内容
  sendhtml?: string | undefined;//发送文本html
}

interface setMeali {
  host: string | undefined | 'smtp.qq.com';
  port: number;
  secure: boolean;
  user: string;
  pass: string;

}

class emailsend {

  static EmailConfig(obj: setMeali): any {
    const { host, port, secure, user, pass } = obj;
    let transporter: Object = nodemailer.createTransport({
      host,  //发送邮箱
      port, //邮箱端口号
      secure, // true for 465, false for other ports
      auth: {
        user, // generated ethereal user  发送的邮箱地址
        pass // generated ethereal password   mtp验证码
      }
    });
    return transporter
  }
  static sedEmali(Emali: getdata): object {
    const { sendout, addressee, sendtitle, sendhtml, sendtxt } = Emali;
    return {
      from: '' + sendout + '<1239184745@qq.com>', // sender address
      to: addressee, // list of receivers  发送地址 可以群发 也可以单独发
      subject: sendtitle, // Subject lineqq.con
      text: sendtxt, // plain text body
      html: sendhtml // html body
    }

  }
  static sendEnter(congif: setMeali, setdata: getdata, cb) {
    const { host, port, secure, user, pass } = congif;//邮箱连接池设置
    const {sendout,addressee,sendtitle,sendhtml,sendtxt}=setdata;
    const csofig = emailsend.EmailConfig({ host, port, secure, user, pass });
    const data = emailsend.sedEmali({sendout,addressee,sendtitle,sendhtml,sendtxt});

    csofig.sendMail(data, (err, info) => {
      cb(err, info)
    })
  }


}


// emailsend.sendEnter(
//   { host: 'smtp.qq.com', //发送邮箱类型
//     port: 465,//端口号
//     secure: true, //true for 465, false for other ports
//     user: '1239184745@qq.com', //发送的邮箱地址
//     pass: 'sghycejeggmibabi' // mtp验证码
//   }, {
//     sendout: '开发者', //发送地址标题
//     addressee: '321071784@qq.com,1239184745@qq.com', //发送地址，可以多个以逗号隔开
//     sendtitle: '你的验证码', //发送标题
//     sendhtml: '<img src="http://imagse1.fcrenti.com/photo/picss/201928/201928212739625.jpg" alt="[MiStar魅妍社]2015.12.09 VOL.037 Moa小姐苏梅旅拍写真 少女" border="0"><img src="http://imagse1.fcrenti.com/photo/picss/201929/201929222834376.jpg" alt="[MiStar魅妍社]2016.02.03 VOL.059 FoxYini孟狐狸 辣妹" border="0">', //发送html内容
//     sendtxt:'12350', //发送文本内容
//    }, (err: object, data: object) => {  //回调函数
//     if(err){
//       return console.log(err)
//     }
//     console.log(data)
// })
let  Emali = emailsend.sendEnter
export  {Emali}